//
//  XSHomeTableViewController.swift
//  SwiftProject
//
//  Created by 杨晓生 on 16/8/3.
//  Copyright © 2016年 XS. All rights reserved.
//

import UIKit

class XSHomeTableViewController: XSBaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if !login{
            //1.如果没有登录，就设置未登录界面信息
            visitorView?.setupVisitorInfo(true, imageName: "visitordiscover_feed_image_house", message: "关注一些人，回这里看看有什么惊喜")
            return
        }
        
        //2.有登陆的导航栏样式
        setupNav()
        
        //3.注册通知，监听菜单
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "change", name: XSPopverAnimatorWillShow, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "change", name: XSPopverAnimatorWillDismiss, object: nil)
        
    }
    
    // 移除通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    func change(){
        // 修改标题按钮的状态
        let titleBtn = navigationItem.titleView as! UIButton
        titleBtn.selected = !titleBtn.selected
    }

    func setupTitleView(){
        let titleView = XSTitleView()
        titleView.setTitle("极客江南 ", forState: UIControlState.Normal)
        titleView.addTarget(self, action: "titleViewClick:", forControlEvents: UIControlEvents.TouchUpInside)
        navigationItem.titleView = titleView
    }
    
    func titleViewClick(btn:XSTitleView){
        //1.修改箭头方向
//        btn.selected = !btn.selected
        
        //2。弹出菜单
        let sb = UIStoryboard(name: "Popver", bundle: nil)
        let vc = sb.instantiateInitialViewController()
        //2.1设置转场代理（iOS8.0 后才有的）
        // 默认情况下modal会移除以前控制器的view, 替换为当前弹出的view
        // 如果自定义转场, 那么就不会移除以前控制器的view
        vc?.transitioningDelegate = popverAnimator
        vc?.modalPresentationStyle = UIModalPresentationStyle.Custom
        
        self.presentViewController(vc!, animated: true, completion: nil)
    }
    
    private lazy var popverAnimator : XSPopverAnimator = {
        let popverAnim = XSPopverAnimator()
        popverAnim.presentFrame = CGRect(x: (UIScreen.mainScreen().bounds.width - 180) * 0.5, y: 56, width: 180, height: 260)
        return popverAnim
    }()
    
    
    func setupNav(){
        //使用UIBarButtonItem的延展来创建UIBarButtonItem
        navigationItem.leftBarButtonItem = UIBarButtonItem.creatBarButtonItem("navigationbar_friendattention", target: self, action: "leftItemClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem.creatBarButtonItem("navigationbar_pop", target: self, action: "rightItemClick")
        
        //3.设置titleView
        setupTitleView()

    }
    
    func leftItemClick(){
        print(__FUNCTION__)
    }
    
    func rightItemClick(){
        print(__FUNCTION__)
    }

    
}





